package org.smile.jstl.tags.pager;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
/**
 * 默认分页条的标签
 * @author strive
 */
public class CommonPagerTag extends PageTagSupport {
	/**
	 * 是否存在首页和末页
	 */
	private boolean hasFirstLast=true;
	/**
	 * 是否动态每页条数
	 */
	private boolean synamicSizes=false;
	/**
	 * 是否存在前一页和后一页
	 */
	private boolean hasPrevNext=true;
	/**
	 * 是否存在页码
	 */
	private boolean hasPages=true;
	/**
	 * 第一个页面、最后一个页码 与中间页面集合 的省略间距 
	 */
	private int omitCount=0;
	/**
	 * 是否存在第一个页码和最后一个页码
	 */
	private boolean hasPageFirstLast=true;
	/**
	 * 是否存在详细信息
	 */
	private boolean hasDescript=true;
	
	private String firstHtml="首页";
	
	private String lastHtml="末页";
	
	private String prevHtml="上一页";
	
	private String nextHtml="下一页";
	
	private String currentHtml="<font color=\"red\">{0}</font>";
	
	private String description="  当前是{0}/{1}页 共{2}条数据 每页显示{3}条 当前显示第{4}至第{5}条";
	/**
	 * 是否动态隐藏 首页、前一页 ……
	 */
	private boolean dynamicHide=false;
	/**
	 * 在标签开始时 打印出分页工具条
	 */
	public int doStartTag() throws JspException {
		super.doStartTag();
		 JspWriter writer = pageContext.getOut(); 
		 //当前页
		int page=pagerTag.getPageNumber();
		//第一个显示item
		int firstPage = pagerTag.getFirstIndexPage();
		//最后一个显示item
		int lastPage=pagerTag.getLastIndexPage(firstPage);
		//总页数
		int pageCount=pagerTag.getPageCount();                                             
		try{
			if(hasFirstLast&&(!dynamicHide||page>0)){
				writer.println("<a href=\""+getPageUrl(0)+"\">"+firstHtml+"</a>");
			}
			if(hasPrevNext&&(!dynamicHide||page>0)){
				writer.println("<a href=\""+getPageUrl(page>0?page-1:0)+"\">"+prevHtml+"</a>");
			}
			if(hasPageFirstLast&&firstPage>omitCount){
				writer.println("<a href=\""+getPageUrl(0)+"\">1</a>...");
			}
			if(hasPages){
				for(int i=firstPage;i<=lastPage;i++){
					if(i!=page){
						writer.println("<a href=\""+getPageUrl(i)+"\">"+(i+1)+"</a>");
					}else{
						writer.println(currentHtml.replaceAll("\\{ *0 *\\}",Integer.toString(page+1)));
					}
				}
			}
			if(hasPageFirstLast&&pageCount-lastPage>omitCount+1){
				writer.println("...<a href=\""+getPageUrl(pageCount-1)+"\">"+(pageCount)+"</a>");
			}
			if(hasPrevNext&&(!dynamicHide||page<pageCount-1)){
				writer.println("<a href=\""+getPageUrl(page<lastPage-1?page+1:(lastPage<0?0:lastPage))+"\">"+nextHtml+"</a>");
			}
			if(hasFirstLast&&(!dynamicHide||page<pageCount-1)){
				writer.println("<a href=\""+getPageUrl(pageCount>0?pageCount-1:0)+"\">"+lastHtml+"</a>");
			}
			if(hasDescript){
				String str=description;
				str=description.replaceAll("\\{ *0 *\\}",Integer.toString(pageCount>0?page+1:0));
				str=str.replaceAll("\\{ *1 *\\}", Integer.toString(pageCount));
				str=str.replaceAll("\\{ *2 *\\}", Integer.toString(pagerTag.getItems()));
				str=str.replaceAll("\\{ *3 *\\}", Integer.toString(pagerTag.getMaxPageItems()));
				str=str.replaceAll("\\{ *4 *\\}", Integer.toString(pagerTag.getOffset()+1));
				str=str.replaceAll("\\{ *5 *\\}", Integer.toString(pagerTag.getNextOffset()<pagerTag.getItems()?pagerTag.getNextOffset():pagerTag.getItems()));
				writer.println(str);
			}
		}catch(Exception e){
			throw new JspException("pager tag case a exception :",e);
		}
		return EVAL_BODY_INCLUDE;
	}
	/**
	 * 获取页码链接 
	 *   如果是动态每页条数 则会在url中添加一个每页长度的参数
	 * @param i 
	 * @return
	 */
	private String getPageUrl(int i){
		if(synamicSizes){
			int pageItems=pagerTag.getMaxPageItems();
			return pagerTag.getSizeUrl(i*pageItems, String.valueOf(pageItems));
		}else{
			return pagerTag.getPageUrl(i);
		}
	}
	public boolean isHasFirstLast() {
		return hasFirstLast;
	}

	public void setHasFirstLast(boolean hasFirstLast) {
		this.hasFirstLast = hasFirstLast;
	}

	public boolean isHasPrevNext() {
		return hasPrevNext;
	}

	public void setHasPrevNext(boolean hasPrevNext) {
		this.hasPrevNext = hasPrevNext;
	}

	public boolean isHasPages() {
		return hasPages;
	}

	public void setHasPages(boolean hasPages) {
		this.hasPages = hasPages;
	}

	public int getOmitCount() {
		return omitCount;
	}
	
	public String getFirstHtml() {
		return firstHtml;
	}

	public void setFirstHtml(String firstHtml) {
		this.firstHtml = firstHtml;
	}

	public String getLastHtml() {
		return lastHtml;
	}

	public void setLastHtml(String lastHtml) {
		this.lastHtml = lastHtml;
	}

	public String getPrevHtml() {
		return prevHtml;
	}

	public void setPrevHtml(String prevHtml) {
		this.prevHtml = prevHtml;
	}

	public String getNextHtml() {
		return nextHtml;
	}

	public void setNextHtml(String nextHtml) {
		this.nextHtml = nextHtml;
	}

	public String getCurrentHtml() {
		return currentHtml;
	}

	public void setCurrentHtml(String currentHtml) {
		this.currentHtml = currentHtml;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setOmitCount(int omitCount) {
		this.omitCount = omitCount;
	}

	public boolean isHasPageFirstLast() {
		return hasPageFirstLast;
	}

	public void setHasPageFirstLast(boolean hasPageFirstLast) {
		this.hasPageFirstLast = hasPageFirstLast;
	}

	public boolean isHasDescript() {
		return hasDescript;
	}

	public void setHasDescript(boolean hasDescript) {
		this.hasDescript = hasDescript;
	}

	public boolean isDynamicHide() {
		return dynamicHide;
	}

	public void setDynamicHide(boolean dynamicHide) {
		this.dynamicHide = dynamicHide;
	}
	public boolean isSynamicSizes() {
		return synamicSizes;
	}
	public void setSynamicSizes(boolean synamicSizes) {
		this.synamicSizes = synamicSizes;
	}

}
